public class LeetCode1943 {
    public static void main(String[] args) {

    }

    public int longestSubarray(int[] nums) {

        int index1 = 0;
        int index2 = 0;
        int max = 0;
        int n = nums.length;

        int k = 0;
        while (index2 < n){
            if(k < 2) {
                ++index1;
                max = Math.max(max, index2 - index1 - 1);
                --k;
            }

            if(nums[index2] == 0){
                ++k;
            }

            if(k < 2){
                index2++;
            }
        }
        max = Math.max(max, index2 - index1 - 1);
        return max;
    }
}
